package com.ple.app.importdata;

import android.graphics.Matrix;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by cov_neeg on 2016/12/1.
 */

public class Image_opreation {
    /**
     *缩放定义
     */
    private  int mMode;
    private int maxscale=3;
    private float minscal= (float) 0.5;
    final private  int DRAGMODE=1;
    final private int ZOOMMODE=2;
    private PointF mStarpt=new PointF();
    private float mStartdis;
    private PointF mMidpt=new PointF();
    private Matrix mMatrix=new Matrix();
    private Matrix mCurrMatrix=new Matrix();
    //
    public  Image_opreation(final ImageView imageView){



        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                imageView.setScaleType(ImageView.ScaleType.MATRIX);
                switch (event.getAction()&MotionEvent.ACTION_MASK){
                    case MotionEvent.ACTION_DOWN:
                    {
                        mMode=DRAGMODE;
                        mStarpt.set(event.getX(),event.getY());
                        mCurrMatrix=imageView.getImageMatrix();//获得当前图片矩阵
                        break;
                    }
                    case MotionEvent.ACTION_POINTER_DOWN:
                    {
                        mMode=ZOOMMODE;
                        mStartdis=spacing(event);//得到两点件距离
                        midpoint(mMidpt,event);
                        mCurrMatrix=imageView.getImageMatrix();
                        break;
                    }
                    case MotionEvent.ACTION_MOVE:
                    {
                        if(mMode==DRAGMODE){
                            float dx=event.getX()-mStarpt.x;
                            float dy=event.getY()-mStarpt.y;
                            // dx=checkdx(dx);
                            //dy=checkdy(dy);
                            mMatrix.set(mCurrMatrix);
                            mMatrix.postTranslate(dx,dy);//移动图片
                            mStarpt.set(event.getX(),event.getY());
                        }// if(mMode==DRAGMODE)
                        else if(mMode==ZOOMMODE){
                            float enddis=spacing(event);
                            if(enddis>10f){
                                float fcale=enddis/mStartdis;
                                float[]values=new float[9];
                                mCurrMatrix.getValues(values);
                                if(fcale*values[mCurrMatrix.MSCALE_X]>maxscale){
                                    fcale=maxscale/values[mCurrMatrix.MSCALE_X];
                                }
                                else if(fcale*values[mCurrMatrix.MSCALE_X]<minscal){
                                    fcale= (float) (minscal/values[mCurrMatrix.MSCALE_X]);
                                }
                                mMatrix.set(mCurrMatrix);
                                mMatrix.postScale(fcale,fcale,mMidpt.x,mMidpt.y);
                                mStartdis=enddis;
                            }
                        }//else if()
                        imageView.setImageMatrix(mMatrix);
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    {

                    }
                    case MotionEvent.ACTION_POINTER_UP:
                    {
                        mMode=0;
                        break;
                    }
                }//switch
                return false;
            }
        });


    }
    /**
     *两点间距离
     */
    public float spacing(MotionEvent event){
        float x=event.getX(0)-event.getX(1);
        float y=event.getY(0)-event.getY(1);
        float dis= (float) Math.sqrt(x*x+y*y);
        return dis;
    }
    /**
     * 中间坐标
     */
    public void midpoint(PointF pointF,MotionEvent event){
        float x=event.getX(0)+event.getX(1);
        float y=event.getY(0)+event.getY(1);
        pointF.set(x/2,y/2);
    }
}
